<template>
  <el-card class="category-container">
    <template #header>
      <div class="header">
        <el-button type="primary" :icon="Plus" @click="handleAdd">增加</el-button>
        <el-popconfirm title="确定删除吗？" confirmButtonText='确定' cancelButtonText='取消' @confirm="handleDelete">
          <template #reference>
            <el-button type="danger" :icon="Delete">批量删除</el-button>
          </template>
        </el-popconfirm>
      </div>
    </template>
    <el-table :load="state.loading" ref="multipleTable"
      :data="state.tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)" tooltip-effect="dark"
      style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="categoryName" label="分类名称">
      </el-table-column>
      <el-table-column prop="categoryRank" label="排序值">
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间">
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.categoryId)">修改</a>
          <el-popconfirm title="确定删除吗？" confirmButtonText='确定' cancelButtonText='取消'
            @confirm="handleDeleteOne(scope.row.categoryId)">
            <template #reference>
              <a style="cursor: pointer">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <div class="pages">
      <div class="page_wrap">
        <span class="page_span1">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
            :page-sizes="[10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="state.tableData.length">
          </el-pagination>
        </span>
      </div>
    </div>
    <DialogAddCategory ref='addCate' :reload="getCategory" :type="state.type" />
  </el-card>
</template>

<script setup>
import { onMounted, onUnmounted, reactive, ref, toRefs, watchEffect } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Plus, Delete } from '@element-plus/icons-vue'
import axios from 'axios'
import DialogAddCategory from '@/components/DialogAddCategory.vue'
const currentPage = ref(1)
const total = ref(0)
const pageSize = ref(10)

const handleSizeChange = (val) => {
  console.log(`每页 ${val} 条`);
  currentPage.value = 1;
  pageSize.value = val;
}

const handleCurrentChange = (val) => {
  console.log(`当前页: ${val}`);
  currentPage.value = val;
}
const addCate = ref(null)
const router = useRouter() // 声明路由实例
const route = useRoute() // 获取路由参数
const state = reactive({
  loading: false,
  tableData: [], // 数据列表
  multipleSelection: [], // 选中项
  total: 0, // 总条数
  currentPage: 1, // 当前页
  pageSize: 10, // 分页大小
  type: 'add', // 操作类型
  level: 1,
  parent_id: 0
})
onMounted(() => {
  getCategory()
})

// 获取分类列表
const getCategory = () => {
  state.loading = true
  axios.post('http://localhost:8080/goods/searchCategory2').then(res => {
    state.tableData = res.data
    console.log(state.tableData);
  })
}

// 添加分类
const handleAdd = () => {
  state.type = 'add'
  addCate.value.open()
}
// 修改分类
const handleEdit = (id) => {
  state.type = 'edit'
  addCate.value.open(id)
}
// 选择项
const handleSelectionChange = (val) => {
  state.multipleSelection = val
}
// 批量删除
const handleDelete = () => {
  if (!state.multipleSelection.length) {
    ElMessage.error('请选择项')
    return
  }
  state.multipleSelection.forEach(item => {
    axios.post('http://localhost:8080/goods/deleCategory/' + item.categoryId).then(() => {
      ElMessage.success('删除成功')
      getCategory()
    })
  })

}
// 单个删除
const handleDeleteOne = (id) => {
  axios.post('http://localhost:8080/goods/deleCategory/' + id).then(() => {
    ElMessage.success('删除成功')
    getCategory()
  })
}
</script>

<style></style>